using System;
using System.Collections.Generic;
using System.Text;
using IGlobalist.Cms.Model;
namespace IGlobalist.Cms.Bll
{
    public class UserController : BaseController
    {
        private static IUser ConvertCmsObject2User(ICmsObject obj)
        {
            IUser user = new User();
            user.FromCmsObject(obj);
            return user;
        }
        private static IList<IUser> ConvertListObject2ListUser(IList<ICmsObject> list)
        {
            IList<IUser> users=new List<IUser>();
            if (list != null)
            foreach (ICmsObject obj in list)
            {
                users.Add(ConvertCmsObject2User(obj));
            }

            return users;
        }


        public static IUser GetUser(long id)
        {
            return ConvertCmsObject2User(_Ds.GetObject(id));
        }

        public static IUser GetUser(string username, long classID)
        {
            IList<IUser> list = GetUserList(classID);
            int n = 0;
            bool isFound=false;
            while (n < list.Count && !isFound)
            {
                if (list[n].UserName.Equals(username, StringComparison.OrdinalIgnoreCase))
                {
                    isFound = true;
                    return list[n];
                }else{
                    n++;
                }
            }
            return null;
        }

        public static IList<IUser> GetUserList(long classID)
        {
            return ConvertListObject2ListUser(_Ds.GetObjectListByClassID(classID));
        }

        public static IList<IUser> GetUserListByWhere(string where)
        {
            IList<IUser> uList = ConvertListObject2ListUser(_Ds.GetObjectListAllByName(where));
            return uList;
        }


        public static long InsertUser(IUser user)
        {
            return _Ds.InsertObject((ICmsObject)user);
        }

        public static void UpdateUser(IUser user)
        {
            _Ds.UpdateObject((ICmsObject)user);
        }

        public static void DeleteUser(long id)
        {
            _Ds.DeleteObject(id);
        }
    }
}
